General information about Python

You can get information about python in the Python documentation webpage. These pages are superbly well written, and have adapted information to pretty much any level, from beginners to more advanced users.

We will use Python 2.7, whose documentation can be found here. Usually, results from a search engine for functions within python will guide you these pages, so it would be a good idea if you familiarise yourself with them.

The Python documentation has a section called "Python Enhancement Proposals" (PEPs), which contain very general information about various topics. Some of them might have information most scientific users do not care about. But there are several notable ones, amongst which:

Tutorials

Apart from the tutorial of the Level 1 and Level 2 labs, we also recommend taking a look at the tutorial offered by the python guys.

Information about functions and modules: the help function and the internet

The easiest way of getting information is using the internet. Any search engine will spit out several hundred millions pages worth of information if you search for python help. Obviously, looking in the documentation pages of the particular module you are using would help. Some of these pages are:

These often offer tutorials on different topics, but most of these should be taken as reference pages, in case you want specific information about the contents of different modules, submodules, data types or functions (all of which, in reality, are treated as objects in Python).

If you do not have access to the internet, Don't panic! Most packages (like matplotlib, scipy,...) have been created by people with good documentation skills, or have had to pass some sort of "quality" standard which prompts them to document their code [link here]. Therefore, these packages will come with a complete documentation, usually in the form of docstrings in the scripts.

To access these, you only need to type help(Stuff) in the console and python will look for the docstrings pertaining to this topic or function. The following code will show the information available for the function max in numpy:


In [1]:
from numpy import max as maxi  #The name is changed to keep the builtin MAX function
help(maxi)


Help on function amax in module numpy.core.fromnumeric:

amax(a, axis=None, out=None)
    Return the maximum of an array or maximum along an axis.
    
    Parameters
    ----------
    a : array_like
        Input data.
    axis : int, optional
        Axis along which to operate.  By default flattened input is used.
    out : ndarray, optional
        Alternate output array in which to place the result.  Must be of
        the same shape and buffer length as the expected output.  See
        `doc.ufuncs` (Section "Output arguments") for more details.
    
    Returns
    -------
    amax : ndarray or scalar
        Maximum of `a`. If `axis` is None, the result is a scalar value.
        If `axis` is given, the result is an array of dimension
        ``a.ndim - 1``.
    
    See Also
    --------
    nanmax : NaN values are ignored instead of being propagated.
    fmax : same behavior as the C99 fmax function.
    argmax : indices of the maximum values.
    
    Notes
    -----
    NaN values are propagated, that is if at least one item is NaN, the
    corresponding max value will be NaN as well.  To ignore NaN values
    (MATLAB behavior), please use nanmax.
    
    Examples
    --------
    >>> a = np.arange(4).reshape((2,2))
    >>> a
    array([[0, 1],
           [2, 3]])
    >>> np.amax(a)
    3
    >>> np.amax(a, axis=0)
    array([2, 3])
    >>> np.amax(a, axis=1)
    array([1, 3])
    
    >>> b = np.arange(5, dtype=np.float)
    >>> b[2] = np.NaN
    >>> np.amax(b)
    nan
    >>> np.nanmax(b)
    4.0

Note that this function is different from the builtin max function.


In [2]:
help(max)


Help on built-in function max in module __builtin__:

max(...)
    max(iterable[, key=func]) -> value
    max(a, b, c, ...[, key=func]) -> value
    
    With a single iterable argument, return its largest item.
    With two or more arguments, return the largest argument.

If help is used on modules, it shows the different submodules, data types and functions it contains, often giving more detailed information about their contents.

Other sources of information

One of the best things about Python is the tremendous user base, which provides with questions and answers about the problems they encounter with the language. These are a invaluable resource, as they push the knowledge of python from the "general information" given by manuals and tutorials to the specific case uses.

They usually do this in forums, mailing lists or places like StackOverflow.

Looking for information in these places is a very valid resource, as you should not waste time in problems that have already been solved elsewhere; furthermore, reading someone else's code will help you in your own coding practice. You should be wary, though, as many solutions to problems are quick-and-dirty approaches to problems.

However, posting new questions in these places should be used as the last resort for information, when the manual and search engines have not been your friends (see RTBM).

Finally, if you find a solution to one of the problems that you have stumbled upon with no solution on the web, please consider documenting it by posting the answer in the internet. The world will become a better place.

Exercises

  • Use the help function in your python interpreter to get information about the following functions:
    • float()
    • range()
    • sum()
  • Find, at least, one matplotlib function that allows you to plot a contour graph.